package com.lframework.xingyun.basedata.mappers;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.lframework.starter.web.core.mapper.BaseMapper;
import com.lframework.xingyun.basedata.bo.member.QueryMemberBo;
import com.lframework.xingyun.basedata.entity.Member;
import com.lframework.xingyun.basedata.entity.MemberAccount;
import com.lframework.xingyun.basedata.entity.MemberLevel;
import com.lframework.xingyun.basedata.entity.MemberLevelDetail;
import com.lframework.xingyun.basedata.vo.member.QueryMemberSelectorVo;
import com.lframework.xingyun.basedata.vo.member.QueryMemberVo;
import com.lframework.starter.web.core.annotations.sort.Sort;
import com.lframework.starter.web.core.annotations.sort.Sorts;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import org.apache.ibatis.annotations.Param;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author zmj
 * @since 2021-07-11
 */
public interface MemberMapper extends BaseMapper<Member> {

  /**
   * 查询列表
   *
   * @param vo
   * @return
   */
  @Sorts({
      @Sort(value = "code", autoParse = true),
      @Sort(value = "name", autoParse = true),
      @Sort(value = "createTime", autoParse = true),
      @Sort(value = "updateTime", autoParse = true),
  })
  default IPage<QueryMemberBo> queryPage(QueryMemberVo vo) {
      return defaultJoinWrapper()
              .selectAll(Member.class)
              .selectAs(MemberAccount::getEffectiveScore, "effectiveScore")
              .selectAs(MemberAccount::getFreezeScore, "freezeScore")
              .selectAs(MemberLevel::getLevelName, "levelName")
              .leftJoin(MemberAccount.class, MemberAccount::getMemberId, Member::getId)
              .leftJoin(MemberLevelDetail.class, on -> on
                      .eq(MemberLevelDetail::getMemberId, Member::getId)
                      .eq(MemberLevelDetail::getActivate, true))
              .leftJoin(MemberLevel.class, MemberLevel::getId, MemberLevelDetail::getLevelId)
              .like(StrUtil.isNotBlank(vo.getCode()), Member::getCode, vo.getCode())
              .like(StrUtil.isNotBlank(vo.getName()), Member::getName, vo.getName())
              .eq(Objects.nonNull(vo.getAvailable()), Member::getAvailable, vo.getAvailable())
              .page(PageDTO.of(vo.getPageIndex(), vo.getPageSize()), QueryMemberBo.class);
  }

    /**
     * 查询列表
     *
     * @param vo
     * @return
     */
    @Sorts({
            @Sort(value = "code", autoParse = true),
            @Sort(value = "name", autoParse = true),
            @Sort(value = "createTime", autoParse = true),
            @Sort(value = "updateTime", autoParse = true),
    })
    default List<QueryMemberBo> queryList(QueryMemberVo vo) {
        return defaultJoinWrapper()
                .selectAll(Member.class)
                .selectAs(MemberAccount::getEffectiveScore, "effectiveScore")
                .selectAs(MemberAccount::getFreezeScore, "freezeScore")
                .selectAs(MemberLevel::getLevelName, "levelName")
                .leftJoin(MemberAccount.class, MemberAccount::getMemberId, Member::getId)
                .leftJoin(MemberLevelDetail.class, on -> on
                        .eq(MemberLevelDetail::getMemberId, Member::getId)
                        .eq(MemberLevelDetail::getActivate, true))
                .leftJoin(MemberLevel.class, MemberLevel::getId, MemberLevelDetail::getLevelId)
                .like(StrUtil.isNotBlank(vo.getCode()), Member::getCode, vo.getCode())
                .like(StrUtil.isNotBlank(vo.getName()), Member::getName, vo.getName())
                .eq(Objects.nonNull(vo.getAvailable()), Member::getAvailable, vo.getAvailable())
                .list(QueryMemberBo.class);
    }

  /**
   * 选择器
   *
   * @param vo
   * @return
   */
  List<Member> selector(@Param("vo") QueryMemberSelectorVo vo);

  /**
   *
   * @param available
   * @return
   */
  default List<String> selectByAvailable(Boolean available) {
      return defaultJoinWrapper()
              .select(Member::getId)
              .eq(Member::getAvailable, available)
              .list().stream()
              .map(Member::getId)
              .collect(Collectors.toList());
  }
}
